/* $Id:members.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "members"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "members.h"
#include "unit.h"
#include "../general/general.h"

members::members() {
    member=0;
    status=0;
    training=0;
}

// Field : member

class unit& members::getMember() const { if (member==0) throw nullpointer(); return *member; }
void members::setMember(class unit &v) { member=&v; }

// Field : status

const int members::getStatus() const { return status; }
void members::setStatus(const int v) { status=v; }

// Field : training

const int members::getTraining() const { return training; }
void members::setTraining(const int v) { training=v; }

void members::save(class channel &ch) {
    ch.write("members", "");
    ch.write("m", getMember().getName());
    ch.write("s", getStatus());
    ch.write("t", getTraining());
}

void members::load(class channel &ch) {
    if (ch.key=="m" || ch.key=="member") {
        if (!unit_repo.isUnit(ch.value)) {
            dbg_state("link unknown unit %s", ch.value.c_str());
            class unit rec;
            rec.setName(ch.value);
            unit_repo.setUnit(rec);
        }
        setMember(unit_repo.getUnit(ch.value));
        ch.next();
    }
    if (ch.key=="s" || ch.key=="status") { setStatus(atoi(ch.value.c_str())); ch.next(); }
    if (ch.key=="t" || ch.key=="training") { setTraining(atoi(ch.value.c_str())); ch.next(); }
}

void members::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<members status=\"" << getStatus() <<
    "\" training=\"" << getTraining() <<
    "\">" << endl;
    if (r<2) try {
        getMember().xml(f,l+1,r+1);
    } catch (nullpointer n) {}
    f << ncpy(l, "  ") << "</members>" << endl;
}
